<?php

use App\Http\Models\Admin\FileLog;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


if (!function_exists('_lang')) {
    function _lang($substitute = 'name')
    {
        $request = request();
        // var_dump($request->segment(3));die;
        return trans($request->segment(1) . '.' . $request->segment(2) . '.' . $request->segment(3) . '.' . $substitute);
    }
}

// sql拼接
function redaySql()
{
    DB::connection()->enableQueryLog();
}
function showSql()
{
    $sqlDb = DB::getQueryLog();
    $resultsql = [];
    foreach ($sqlDb as  $sql_k => $sql_arr) {
        $resultsql[$sql_k]['sql'] = '';
        $resultsql[$sql_k]['time'] = $sql_arr['time'];

        $split = preg_split("(\?)", $sql_arr['query']);

        for ($i = 0; $i < count($split); $i++) {

            if (array_key_exists($i, $sql_arr['bindings'])) {
                if (is_string($sql_arr['bindings'][$i])) {
                    $sql_arr['bindings'][$i] = "'" . $sql_arr['bindings'][$i] . "'";
                }
                $resultsql[$sql_k]['sql'] .= $split[$i] . $sql_arr['bindings'][$i];
            } else {
                $resultsql[$sql_k]['sql'] .= $split[$i];
            }
        }
    }
    print_r($resultsql);
}

// 数组去重
function assoc_unique($arr, $key)
{
    $tmp_arr = array();
    foreach ($arr as $k => $v) {
        if (in_array($v[$key], $tmp_arr)) { //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
            unset($arr[$k]);
        } else {
            $tmp_arr[] = $v[$key];
        }
    }
    sort($arr); //sort函数对数组进行排序
    return $arr;
}
// 数组排序
function arraySort($array, $keys, $sort = 'asc')
{
    $newArr = $valArr = array();
    foreach ($array as $key => $value) {
        $valArr[$key] = $value[$keys];
    }
    ($sort == 'asc') ?  asort($valArr) : arsort($valArr);
    reset($valArr);
    foreach ($valArr as $key => $value) {
        $newArr[$key] = $array[$key];
    }
    return $newArr;
}

// 获取排序相关

function getSort($request)
{

    $sort = $request->get('sort');

    $sort_rank_txt = ['+' => 'ASC', '-' => 'DESC'];
    $arr = preg_split("/(\+|\-)/", $sort, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

    $arr[0] = $sort_rank_txt[$arr[0]];
    $arr = array_reverse($arr);
    return $arr;
}
// 获取钉钉配置

if (!function_exists('getDDingSetting')) {
    function getDDingSetting()
    {
        $config = [
            'app_key' => env('DDING_APP_KEY'),
            'app_secret' => env('DDING_APP_SECRET'),
            'agent_id' => env('DDING_AGENT_ID'),
            'corp_id' => env('DDING_CORP_ID'),
            'nonce_str' => env('DDING_NONCESTR')
        ];
        return $config;
    }
}
